﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using OneMake.DataAccess;
using OneMake.Model;

namespace OneMake.Winforms
{
    public partial class BaseDataForm : Form
    {
        private DataHelper dataHelper = new DataHelper();
        public int tabIndex { get; set; }
        public BaseDataForm()
        {
            InitializeComponent();

            dtpBegin.Value = DateTime.Now.AddDays(-10);
            dtpEnd.Value = DateTime.Now;

            cbEmpList.DataSource = dataHelper.GetEmployees();
            cbEmpList.DisplayMember = "employee_no";
            cbEmpList.ValueMember = "employeeid";
        }

        private void BaseDataForm_Shown(object sender, EventArgs e)
        {
            if (tabIndex == 1)
                tabControl1.SelectedTab = tabPage1;
            else if (tabIndex == 2)
                tabControl1.SelectedTab = tabPage2;
            else
                tabControl1.SelectedTab = tabPage3;

            using (var db = new PetaPoco.Database("mysql" + UserSession.Network))
            {
                string sql = "SELECT * FROM employee_info";
                DataTable table = db.ExecuteTable(sql);

                Dictionary<string, string> attrMap = dataHelper.GetTableFieldMap("employee_info");
                foreach (KeyValuePair<string, string> item in attrMap)
                {
                    table.Columns[item.Key].ColumnName = item.Value;
                }
                empDataGrid.DataSource = table;
            }
        }




        private void btnRefreshLog_Click(object sender, EventArgs e)
        {
            int employeeid = (int)((employee_info)cbEmpList.SelectedItem).employeeid;

            Dictionary<string, string> attrMap = dataHelper.GetTableFieldMap("log_operation");
            DataTable table = dataHelper.GetLogs(employeeid, dtpBegin.Value, dtpEnd.Value);
            foreach (KeyValuePair<string, string> item in attrMap)
            {
                table.Columns[item.Key].ColumnName = item.Value;
            }
            dgvLogs.DataSource = table;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

        }

        private void btnDel_Click(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

        }
    }
}
